package com.yjm.test.Spring5.ch4.applicationEvent;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Publisher implements ApplicationContextAware {
	private ApplicationContext ctx;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		ctx = applicationContext;
	}

	public void publish(String msg) {
		ctx.publishEvent(new MessageEvent(this,msg));
	}
	
	public static void main(String...args) {
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/ch4/message-event.xml");
		
		Publisher pub = (Publisher) applicationContext.getBean("publisher");
		pub.publish("I send an SOS to the world... ");
		pub.publish("...I hope that sameone gets my...");
		pub.publish("... Message in a bottle");
	}
}
